package chuichui.resource
{
	import chuichui.Utils.Print;
	import chuichui.data.ResData;

	import flash.events.EventDispatcher;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;

	/**
	 * 按照配置文件加载
	 * @author hyy
	 *
	 */
	public class PreLoad extends EventDispatcher
	{
		private static var instance : PreLoad;
		/**
		 * 加载配置xml的加载器
		 */
		private var resource : XMLResource;
		/**
		 * 资源根目录
		 */
		public var resUrl : String;
		/**
		 * 场景地址
		 */
		public var sceneUrl : String;
		/**
		 * 声音地址
		 */
		public var soundUrl : String;
		public var frameRate : int;
		/**
		 * 图标加载地址
		 */
		public var icoUrl : String;
		/**
		 *  连接服务器的地址
		 */
		public var ip : String;
		/**
		 * 是否debug模式
		 */
		public var debug : Boolean;
		/**
		 * 端口
		 */
		public var port : int;
		/**
		 * 每个资源地址,版本号,id
		 */
		public var resData : Dictionary;
		/**
		 * 批量加载地址
		 */
		private var batchData : Dictionary;

		protected var returnComplete : Function;

		protected var resources : Resources;

		public function PreLoad()
		{
			resources = Resources.getInstance();
		}


		public static function getInstance() : PreLoad
		{
			if(instance == null)
			{
				instance = new PreLoad();
			}
			return instance;
		}

		/**
		 * 加载配置文件
		 * @param url 加载地址
		 * @param returnConfigComplete 返回函数
		 *
		 */
		public function loadConfig(url : String, returnComplete : Function) : void
		{
			this.resource = new XMLResource(url, onConfigComplete);
			this.returnComplete = returnComplete;
			this.resource.load();
		}


		/**
		 * 加载成功
		 * @param res
		 *
		 */
		protected function onConfigComplete(res : Resource) : void
		{
			this.parseConfigData(resource.xml);
			this.parseBatchData(resource.xml);
			this.returnComplete(this);
			this.resource = null;
		}

		/**
		 * 解析加载地址
		 * @param xml
		 *
		 */
		protected function parseConfigData(xml : XML) : void
		{
			debug = xml.debug[0] == "true";
			frameRate = xml.frameRate[0];
			ip = xml.ip[0].toString();
			port = xml.port[0];
			resUrl = xml.url[0].toString();
			icoUrl = xml.icoUrl[0].toString();
			sceneUrl = xml.sceneUrl[0].toString();
			soundUrl = xml.soundUrl[0].toString();

			resData = new Dictionary();
			var xmlList : XMLList = xml.file;
			var len : int = xmlList.length();
			var resourceData : ResData;

			for(var i : int = 0; i < len; i++)
			{
				var child : XML = xmlList[i];
				var id : String = child.@id;
				var url : String = resUrl + child.@url;
				var version : int = child.@version;
				var type : String = url.split(".").pop();
				resourceData = new ResData(url, type, 0, version);
				resData[id] = resourceData;
			}
		}

		/**
		 * 解析批量下载id
		 *
		 */
		protected function parseBatchData(xml : XML) : void
		{
			batchData = new Dictionary();
			var batchArray : Array;
			var xmlList : XMLList = xml.batch;
			var len : int = xmlList.length();

			for(var i : int = 0; i < len; i++)
			{
				var child : XML = xmlList[i];
				//预加载的id
				var id : String = child.@id;
				var count : int = child.file.length();
				//id所对应的一系列资源地址
				batchArray = [];

				for(var j : int = 0; j < count; j++)
				{
					batchArray.push(child.file[j].@id.toString());
				}
				batchData[id] = batchArray;
			}
		}

		/**
		 * 根据id进行批量加载
		 * @param id
		 * @param callFun
		 *
		 */
		public function bathLoad(id : String, callFun : Function = null, onProgress : Function = null, context : LoaderContext = null) : void
		{
			var loads : Array = id.split(",");
			resources.reset();

			for each(var key : String in loads)
			{
				addResourceUrl(key);
			}

			resources.startLoad(callFun, onProgress, context);
		}

		/**
		 * 添加资源地址
		 * @param id
		 *
		 */
		public function addResourceUrl(id : String) : void
		{
			var tmpArr : Array = batchData[id];
			var len : int = tmpArr.length;
			var resourceData : ResData;

			for(var i : int = 0; i < len; i++)
			{
				var loadID : String = tmpArr[i];
				resourceData = resData[loadID];
				resources.add(resourceData);
			}
		}

	}
}